package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMaterialBean;
import com.koron.css2.newProjectCenter.bean.PjMeterMaterialBean;
import com.koron.css2.newProjectCenter.bean.query.PjMaterialQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjMaterialMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMaterialMapper;
import com.koron.css2.newProjectCenter.service.PjInfoService;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.NumberUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 施工勘察-水表材料单保存
 */
public class PjMeterMaterialSave implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            List<PjMeterMaterialBean> pjMaterialList = JsonUtils.objectToList(req.getData(), PjMeterMaterialBean.class);
            PjMeterMaterialMapper pjMaterialMapper = factory.getMapper(PjMeterMaterialMapper.class);
            PjMaterialMapper materialMapper = factory.getMapper(PjMaterialMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);

            // 将材料主键ID转化为列表集合
            List<String> pjMaterialIdList = pjMaterialList.stream()
                    .map(t -> t.getPjMaterialId())
                    .collect(Collectors.toList());
            PjMaterialQueryBean query = new PjMaterialQueryBean();
            query.setIdList(pjMaterialIdList);
            // 通过材料主键ID查询出材料单集合
            List<PjMaterialBean> oldPjMaterialList = materialMapper.selectList(query);
            // 将材料单列表集合转成 MAP
            Map<String, PjMaterialBean> oldPjMaterialMap = oldPjMaterialList.stream().collect(Collectors.toMap(PjMaterialBean::getId, Function.identity()));

            // 表前材料费金额
            double budgetMatrMeterBefore = 0.d;
            // 表后材料费金额
            double budgetMatrMeterAfter = 0.d;
            for (int i = 0; i < pjMaterialList.size(); i++){
                PjMeterMaterialBean pjMaterial = pjMaterialList.get(i);
                if (pjMaterial.getPrice() == null){
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL,"第" + (i + 1) + "行材料价格不能为空",null);
                }
                if (pjMaterial.getQuantity() == null){
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL,"第" + (i + 1) + "行材料数量不能为空",null);
                }
                if (pjMaterial.getMaterialMoney() == null){
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL,"第" + (i + 1) + "行材料金额不能为空",null);
                }
                if (pjMaterial.getMaterialType()== null){
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL,"第" + (i + 1) + "行材料类型不能为空",null);
                }
                pjMaterial.setCreateInfo(userInfo);
                // 表前材料 金额累计
                if (pjMaterial.getMaterialType() == PjMeterMaterialBean.MATER_TYPE_BEFORE){
                    budgetMatrMeterBefore = NumberUtils.add(pjMaterial.getMaterialMoney(), budgetMatrMeterBefore);
                }
                // 表后材料 金额累计
                if (pjMaterial.getMaterialType() == PjMeterMaterialBean.MATER_TYPE_AFTER){
                    budgetMatrMeterAfter = NumberUtils.add(pjMaterial.getMaterialMoney(), budgetMatrMeterAfter);
                }
                // 通过材料ID 查询出已存在的材料单
                PjMaterialBean oldPjMaterial = oldPjMaterialMap.get(pjMaterial.getPjMaterialId());
                if (oldPjMaterial != null){
                    pjMaterial.setMaterialName(oldPjMaterial.getMaterialName());
                    pjMaterial.setMaterialNo(oldPjMaterial.getMaterialNo());
                    pjMaterial.setSpecs(oldPjMaterial.getSpecs());
                    pjMaterial.setUnit(oldPjMaterial.getUnit());
                }
            }
            if (pjMaterialList.size() > 0){
                PjInfoBean pjInfo = pjInfoMapper.selectById(pjMaterialList.get(0).getPjId());
                if (Objects.equals(PjInfoService.SURVEY, pjInfo.getProcessState())){
                    pjInfo.setId(pjMaterialList.get(0).getPjId());
                    pjInfo.setBudgetMatrMeterBefore(budgetMatrMeterBefore);
                    pjInfo.setBudgetMatrMeterAfter(budgetMatrMeterAfter);
                    pjInfo.setBudgetMoney(NumberUtils.add(budgetMatrMeterBefore, budgetMatrMeterAfter));
                    pjInfo.setDepositMoney(NumberUtils.add(budgetMatrMeterBefore, budgetMatrMeterAfter));
                    // 修改工程报装单据主表材料费金额
                    pjInfoMapper.updateBudgetMatrMeter(pjInfo);
                }
                // 先批量删除
                pjMaterialMapper.deleteByPjId(pjMaterialList.get(0).getPjId());
                // 在批量添加
                pjMaterialMapper.batchAddPjMeterMaterial(pjMaterialList);
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"工程材料单保存成功",null);
        } catch (Exception e) {
            logger.error("工程材料单保存失败",e);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL,"工程材料单保存失败",null);
    }
}